package com.app.micaihu.view.user.userinfo.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.app.micaihu.R;
import com.app.micaihu.app.AppApplication;
import com.app.micaihu.base.AppBaseAdapter;
import com.app.micaihu.base.BaseOldActivity;
import com.app.micaihu.bean.comment.MyComment;
import com.app.micaihu.bean.comment.NewsComment;
import com.app.micaihu.configure.Config;
import com.app.micaihu.configure.TypeConstant;
import com.app.micaihu.custom.view.CustomImageView;
import com.app.micaihu.listener.AutoWidthControllerListener;
import com.app.micaihu.listener.CommentListener;
import com.app.micaihu.utils.CommentUtils;
import com.app.micaihu.utils.DialogUtils;
import com.app.micaihu.view.comment.SecondaryCommentDetailActivity;
import com.app.micaihu.view.main.topic.PostDetailActivity;
import com.app.micaihu.view.newsdetail.ImageShowActivity;
import com.app.utils.util.ToastUtils;
import com.app.utils.util.ViewSizeUtils;
import com.app.utils.util.image.LoadImageUtils;
import com.app.utils.util.view.expression.view.BiaoQinTextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Guo on 2017/3/16.
 */

public class MyDynamicTopicAdapter extends AppBaseAdapter<MyComment> implements View.OnClickListener {
    private boolean isMine;
    private int thumbSize = 140;//默认的 防止异常

    private CommentListener commentListener;
    private MyComment currentComment = null;


    public MyDynamicTopicAdapter(List<MyComment> list, Context context) {
        super(list, context);
    }

    public MyDynamicTopicAdapter(List<MyComment> list, Context context, boolean isMine) {
        super(list, context);
        this.isMine = isMine;
        this.thumbSize = (int) ViewSizeUtils.changeView30Per(context, 3.0f, 30f);

    }


    @Override
    public View creatView(int position, View convertView, ViewGroup parent) {
        MyDynamicTopicAdapter.ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_dynamic_topic, null);
            viewHolder = new MyDynamicTopicAdapter.ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (MyDynamicTopicAdapter.ViewHolder) convertView.getTag();
        }
        MyComment myComment = list.get(position);
        if (myComment == null) {
            convertView.setVisibility(View.GONE);
            return convertView;
        }
        if (convertView.getVisibility() == View.GONE) {
            convertView.setVisibility(View.VISIBLE);
        }
        convertView.setTag(R.id.tag_first, myComment);


        if (isMine) {
            viewHolder.ivMenu.setVisibility(View.VISIBLE);
            viewHolder.ivMenu.setTag(myComment);
        } else {
            viewHolder.ivMenu.setVisibility(View.GONE);
        }

        viewHolder.llContainer.setTag(R.id.tag_first, myComment);


        viewHolder.tvNickname.setText(myComment.getNickName());
        LoadImageUtils.getInstance().loadImage(viewHolder.civRank, myComment.getRankIcon());
//        LoadImageUtils.getInstance().loadImage(viewHolder.civHeadpic, myComment.getHeadPic());
        LoadImageUtils.getInstance().loadListCircleImage(viewHolder.civHeadpic, myComment.getHeadPic());


        viewHolder.tvRankName.setText(myComment.getRankName());
        viewHolder.tvPublishtime.setText(myComment.getPublishTime());


        if (TypeConstant.COMMENT_PARENT.equals(myComment.getCmtType())) {//我发布的帖子
            showPost(viewHolder, myComment);

        } else if (TextUtils.equals(myComment.getCmtType(), TypeConstant.COMMENT_TWO)//我回复的帖子
                || TextUtils.equals(myComment.getCmtType(), TypeConstant.COMMENT_ONE)) {

            showChildPost(viewHolder, myComment);

        }


        return convertView;
    }


    /**
     * 带回复的
     *
     * @param viewHolder
     * @param myComment
     */
    private void showChildPost(ViewHolder viewHolder, MyComment myComment) {
        viewHolder.postContainer.setVisibility(View.GONE);
        viewHolder.childContainer.setVisibility(View.VISIBLE);
        viewHolder.childContent.setPicText(myComment.getComment());

        viewHolder.postTitle.setPicText("[话题] " + myComment.getArticleTitle());
        if (myComment.getParentThumbList() == null || myComment.getParentThumbList().isEmpty()) {
            viewHolder.childThumbContainer.setVisibility(View.GONE);
        } else {
            viewHolder.childThumbContainer.setVisibility(View.VISIBLE);
            setPostThumbChild(viewHolder, myComment.getParentThumbList());

        }


        viewHolder.childPost.setTag(R.id.tag_first, myComment.getArticleId());
    }

    /**
     * 发布的帖子
     *
     * @param viewHolder
     * @param myComment
     */
    private void showPost(ViewHolder viewHolder, MyComment myComment) {
        viewHolder.postContainer.setVisibility(View.VISIBLE);
        viewHolder.childContainer.setVisibility(View.GONE);
        if (TextUtils.isEmpty(myComment.getArticleTitle())) {
            viewHolder.tvPostTitle.setVisibility(View.GONE);
        } else {
            viewHolder.tvPostTitle.setVisibility(View.VISIBLE);
            viewHolder.tvPostTitle.setText(myComment.getArticleTitle());

        }
        if (TextUtils.isEmpty(myComment.getComment())) {
            viewHolder.tvPostcontent.setVisibility(View.GONE);
        } else {
            viewHolder.tvPostcontent.setVisibility(View.VISIBLE);
            viewHolder.tvPostcontent.setPicText(myComment.getComment());
        }
        List<String> contentThumbList = myComment.getContentThumbList();
        setPostThumb(viewHolder, contentThumbList);
    }


    private void setPostThumb(ViewHolder viewHolder, List<String> imgList) {
        if (imgList == null || imgList.size() <= 0) {
            viewHolder.flAllImgContainer.setVisibility(View.GONE);
        } else {
            viewHolder.flAllImgContainer.setVisibility(View.VISIBLE);
            if (imgList.size() == 1) {
                viewHolder.civThumbBig.setVisibility(View.VISIBLE);
                viewHolder.llImgContainer3.setVisibility(View.GONE);
                LoadImageUtils.getInstance().loadImage(viewHolder.civThumbBig, imgList.get(0), viewHolder.autoWidthControllerListener);
            } else {
                viewHolder.civThumbBig.setVisibility(View.GONE);
                viewHolder.llImgContainer3.setVisibility(View.VISIBLE);
                viewHolder.civThumb0.setVisibility(View.VISIBLE);
                LoadImageUtils.getInstance().loadImage(viewHolder.civThumb0, imgList.get(0));

                viewHolder.civThumb1.setVisibility(View.VISIBLE);
                LoadImageUtils.getInstance().loadImage(viewHolder.civThumb1, imgList.get(1));
                if (imgList.size() > 2) {
                    viewHolder.civThumb2.setVisibility(View.VISIBLE);
                    LoadImageUtils.getInstance().loadImage(viewHolder.civThumb2, imgList.get(2));
                } else {
                    viewHolder.civThumb2.setVisibility(View.GONE);
                }
            }


            if (imgList.size() > 3) {
                viewHolder.tvNum.setText(imgList.size() + "图");
                viewHolder.tvNum.setVisibility(View.VISIBLE);
            } else {
                viewHolder.tvNum.setVisibility(View.GONE);
            }
            //为图片设置tag
            viewHolder.civThumb0.setTag(imgList);
            viewHolder.civThumb1.setTag(imgList);
            viewHolder.civThumb2.setTag(imgList);
            viewHolder.civThumbBig.setTag(imgList);
        }

    }


    private void setPostThumbChild(ViewHolder viewHolder, List<String> imgList) {

        if (imgList.size() == 1) {
            LoadImageUtils.getInstance().loadImage(viewHolder.civPostThumb0, imgList.get(0));
            viewHolder.civPostThumb0.setVisibility(View.VISIBLE);
            viewHolder.civPostThumb1.setVisibility(View.GONE);
            viewHolder.civPostThumb2.setVisibility(View.GONE);
        } else {
            viewHolder.civPostThumb0.setVisibility(View.VISIBLE);
            LoadImageUtils.getInstance().loadImage(viewHolder.civPostThumb0, imgList.get(0));

            viewHolder.civPostThumb1.setVisibility(View.VISIBLE);
            LoadImageUtils.getInstance().loadImage(viewHolder.civPostThumb1, imgList.get(1));
            if (imgList.size() > 2) {
                viewHolder.civPostThumb2.setVisibility(View.VISIBLE);
                LoadImageUtils.getInstance().loadImage(viewHolder.civPostThumb2, imgList.get(2));
            } else {
                viewHolder.civPostThumb2.setVisibility(View.GONE);
            }
        }


        //为图片设置tag
        viewHolder.civPostThumb0.setTag(imgList);
        viewHolder.civPostThumb1.setTag(imgList);
        viewHolder.civPostThumb2.setTag(imgList);

    }


    private HashMap<String, Object> dialogParams;
    private View deleteView;

    class ViewHolder {

        LinearLayout llContainer;

        CustomImageView civHeadpic;//头像
        TextView tvNickname;//昵称
        TextView tvPublishtime;//发布时间
        TextView tvRankName;//发帖人的军衔名称
        CustomImageView civRank;//军衔
        ImageView ivMenu;//操作按钮

        TextView tvPostTitle;//话题的title
        BiaoQinTextView tvPostcontent;//话题内容

        FrameLayout flAllImgContainer;
        LinearLayout llImgContainer3;
        CustomImageView civThumb0;
        CustomImageView civThumb1;
        CustomImageView civThumb2;
        CustomImageView civThumbBig;

        AutoWidthControllerListener autoWidthControllerListener;

        AutoWidthControllerListener.OnLoadCompeleted onLoadCompeleted;


        TextView tvNum;


        LinearLayout postContainer;
        LinearLayout childContainer;
        BiaoQinTextView childContent;
        BiaoQinTextView postTitle;
        CustomImageView civPostThumb0;
        CustomImageView civPostThumb1;
        CustomImageView civPostThumb2;
        LinearLayout childThumbContainer;
        LinearLayout childPost;

        public ViewHolder(View convertView) {

            llContainer = (LinearLayout) convertView.findViewById(R.id.ll_item_post);

            postContainer = (LinearLayout) convertView.findViewById(R.id.parent_container);


            civHeadpic = (CustomImageView) convertView.findViewById(R.id.civ_headpic);
            tvNickname = (TextView) convertView.findViewById(R.id.tv_nickname);
            tvPublishtime = (TextView) convertView.findViewById(R.id.tv_publishtime);


            tvRankName = (TextView) convertView.findViewById(R.id.tv_rankname);
            civRank = (CustomImageView) convertView.findViewById(R.id.civ_rank);


            ivMenu = (ImageView) convertView.findViewById(R.id.iv_menu);


            tvPostTitle = (TextView) convertView.findViewById(R.id.tv_posttitle);
            tvPostcontent = (BiaoQinTextView) convertView.findViewById(R.id.tv_postcontent);


            flAllImgContainer = (FrameLayout) convertView.findViewById(R.id.fl_imagecontainer);
            llImgContainer3 = (LinearLayout) convertView.findViewById(R.id.ll_image_container3);
            civThumb0 = (CustomImageView) convertView.findViewById(R.id.civ_thumb0);
            civThumb1 = (CustomImageView) convertView.findViewById(R.id.civ_thumb1);
            civThumb2 = (CustomImageView) convertView.findViewById(R.id.civ_thumb2);
            ViewSizeUtils.changeViewWH(civThumb0, thumbSize, thumbSize);
            ViewSizeUtils.changeViewWH(civThumb1, thumbSize, thumbSize);
            ViewSizeUtils.changeViewWH(civThumb2, thumbSize, thumbSize);

            civThumbBig = (CustomImageView) convertView.findViewById(R.id.civ_big_thumb);


            //回复帖子的
            childContainer = (LinearLayout) convertView.findViewById(R.id.child_container);
            childContent = (BiaoQinTextView) convertView.findViewById(R.id.tv_child_content);
            postTitle = (BiaoQinTextView) convertView.findViewById(R.id.child_post_title);
            civPostThumb0 = (CustomImageView) convertView.findViewById(R.id.civ_paren_thumb0);
            civPostThumb1 = (CustomImageView) convertView.findViewById(R.id.civ_paren_thumb1);
            civPostThumb2 = (CustomImageView) convertView.findViewById(R.id.civ_paren_thumb2);

            ViewSizeUtils.changeViewWH(civPostThumb0, thumbSize, thumbSize);
            ViewSizeUtils.changeViewWH(civPostThumb1, thumbSize, thumbSize);
            ViewSizeUtils.changeViewWH(civPostThumb2, thumbSize, thumbSize);
            childPost = (LinearLayout) convertView.findViewById(R.id.child_post);
            childThumbContainer = (LinearLayout) convertView.findViewById(R.id.child_thumb_container);


            childPost.setOnClickListener(MyDynamicTopicAdapter.this);
            civPostThumb0.setOnClickListener(MyDynamicTopicAdapter.this);
            civPostThumb1.setOnClickListener(MyDynamicTopicAdapter.this);
            civPostThumb2.setOnClickListener(MyDynamicTopicAdapter.this);

            convertView.setOnClickListener(MyDynamicTopicAdapter.this);
            civThumb0.setOnClickListener(MyDynamicTopicAdapter.this);
            civThumb1.setOnClickListener(MyDynamicTopicAdapter.this);
            civThumb2.setOnClickListener(MyDynamicTopicAdapter.this);
            civThumbBig.setOnClickListener(MyDynamicTopicAdapter.this);
            llContainer.setOnClickListener(MyDynamicTopicAdapter.this);

            //设置监听
            autoWidthControllerListener = new AutoWidthControllerListener(civThumbBig);
            ivMenu.setOnClickListener(MyDynamicTopicAdapter.this);
            tvNum = (TextView) convertView.findViewById(R.id.tv_num);

            onLoadCompeleted = new AutoWidthControllerListener.OnLoadCompeleted() {
                @Override
                public void onLoadCompeleted(CustomImageView civ, boolean flag) {
                    if (flag) {
                        tvNum.setVisibility(View.VISIBLE);
                        tvNum.setText(AppApplication.getInstance().getString(R.string.longpic));
                    } else {
                        tvNum.setVisibility(View.GONE);
                    }

                }
            };
            autoWidthControllerListener.setOnLoadCompeleted(onLoadCompeleted);
        }


    }


    @Override
    public void onClick(View v) {
        if (v == null) return;
        switch (v.getId()) {
            case R.id.iv_menu://点击item菜单
                if (v.getTag() == null || !(v.getTag() instanceof MyComment)) return;
                MyComment myComment = (MyComment) v.getTag();

                showDelete(myComment);
                break;

            case R.id.tv_cancel:
                DialogUtils.getInstance().dismissCDialog();
                break;
            case R.id.tv_save:
                DialogUtils.getInstance().dismissCDialog();
                requestNet2Delete();
                break;

            case R.id.civ_big_thumb:
            case R.id.civ_thumb0:
                if (v.getTag() instanceof List) {
                    ArrayList imgList = (ArrayList) v.getTag();
                    showImageDetail(imgList, 0);
                }

                break;
            case R.id.civ_thumb1:
                if (v.getTag() instanceof List) {
                    ArrayList imgList = (ArrayList) v.getTag();
                    showImageDetail(imgList, 1);
                }

                break;
            case R.id.civ_thumb2:
                if (v.getTag() instanceof List) {
                    ArrayList imgList = (ArrayList) v.getTag();
                    showImageDetail(imgList, 2);
                }

                break;

            case R.id.ll_item_post:
                try {
                    MyComment tag = (MyComment) v.getTag(R.id.tag_first);

                    goToCommentDetail(tag);

                } catch (Exception e) {

                }
                break;
            case R.id.civ_paren_thumb0:
                if (v.getTag() instanceof List) {
                    ArrayList imgList = (ArrayList) v.getTag();
                    showImageDetail(imgList, 0);
                }
                break;
            case R.id.civ_paren_thumb1:
                if (v.getTag() instanceof List) {
                    ArrayList imgList = (ArrayList) v.getTag();
                    showImageDetail(imgList, 1);
                }
                break;
            case R.id.civ_paren_thumb2:
                if (v.getTag() instanceof List) {
                    ArrayList imgList = (ArrayList) v.getTag();
                    showImageDetail(imgList, 2);
                }
                break;


            case R.id.child_post://回复的帖子
                try {
                    if (context == null) return;
                    String parentId = (String) v.getTag(R.id.tag_first);
                    Intent intent = new Intent();

                    intent.setClass(context, PostDetailActivity.class);
                    intent.putExtra(Config.Extra.PARAMETER1, parentId);
                    context.startActivity(intent);
                } catch (Exception e) {

                }
                break;

        }
    }


    /**
     * 展示大图
     *
     * @param imgList
     * @param i
     */
    private void showImageDetail(ArrayList imgList, int i) {
        Intent intent = new Intent(context, ImageShowActivity.class);
        intent.putStringArrayListExtra(Config.Extra.PARAMETER1, imgList);
        intent.putExtra(Config.Extra.PARAMETER2, i);
        intent.setClass(context, ImageShowActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (context instanceof Activity) {
            context.startActivity(intent);
            ((Activity) context).overridePendingTransition(R.anim.base_alpha_in, R.anim.base_slide_remain);// 前一个参数是下一个Activity的效果
        }


    }

    private void showDelete(MyComment myComment) {
        currentComment = myComment;
        if (deleteView == null) {
            deleteView = LayoutInflater.from(context).inflate(R.layout.dialog_save_image, null);
            TextView cancel = (TextView) deleteView.findViewById(R.id.tv_cancel);
            TextView save = (TextView) deleteView.findViewById(R.id.tv_save);
            cancel.setText(AppApplication.getInstance().getString(R.string.common_cancel));
            cancel.setTextColor(AppApplication.getInstance().getResources().getColor(R.color.common_main_textcolor));
            save.setText(AppApplication.getInstance().getString(R.string.common_delete));
            save.setTextColor(AppApplication.getInstance().getResources().getColor(R.color.common_red_color));
            cancel.setOnClickListener(this);
            save.setOnClickListener(this);
        }
        if (dialogParams == null) {
            dialogParams = new HashMap<>();
            dialogParams.put("anim", R.style.dialogWindowAnim);
            dialogParams.put("local", Gravity.BOTTOM);
        }
        DialogUtils.getInstance().showCDialog(deleteView, context, dialogParams);
    }


    /**
     * 跳转到评论详情
     *
     * @param tag1
     */
    private void goToCommentDetail(MyComment tag1) {
        if (tag1 == null || context == null) return;
        Intent intent1 = new Intent();
        if (TypeConstant.POST_2052.equals(tag1.getArticleType())) {
            if (TextUtils.equals(tag1.getCmtType(), TypeConstant.COMMENT_PARENT)) {
                intent1.setClass(context, PostDetailActivity.class);
                intent1.putExtra(Config.Extra.PARAMETER1, tag1.getCommentId());
            } else if (TextUtils.equals(tag1.getCmtType(), TypeConstant.COMMENT_TWO)
                    || TextUtils.equals(tag1.getCmtType(), TypeConstant.COMMENT_ONE)) {
                intent1.setClass(context, SecondaryCommentDetailActivity.class);
                intent1.putExtra(Config.Extra.PARAMETER1, tag1.getArticleId());
                intent1.putExtra(Config.Extra.PARAMETER2, tag1.getParentCmtId());
                intent1.putExtra(SecondaryCommentDetailActivity.COMMENT_OR_POST, false);
            }
        }
        if (intent1.getComponent() != null) {
            context.startActivity(intent1);
        }
    }


    /**
     * 访问网络删除评论
     */
    private void requestNet2Delete() {
        if (currentComment == null) return;
        if (commentListener == null) {
            commentListener = new CommentListener() {
                @Override
                public void commentStart() {
                    DialogUtils.getInstance().showProgressDialog((Activity) context, AppApplication.getInstance().getResources().getString(R.string.homepage_attention_start));
                }

                @Override
                public void commentFail(VolleyError volley) {
                    DialogUtils.getInstance().dismissProgressDialog();
                    ToastUtils.makeText(AppApplication.getInstance().getString(R.string.neterror));
                }

                @Override
                public void commentSuccess(NewsComment newsComment) {
                    DialogUtils.getInstance().dismissProgressDialog();
                    ToastUtils.makeText(AppApplication.getInstance().getString(R.string.common_delete_suc));
                    list.remove(currentComment);
                    notifyDataSetChanged();
                    currentComment = null;
                }
            };
        }
        CommentUtils.getInstance().deleteComment(currentComment.getArticleId(), currentComment.getCommentId(), "0", commentListener, "");


    }
}
